package com.example.memento.download.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "download")
public class DownloadProperties {
    private String baseDir = "data/downloads";
    private boolean extract = true;
    
    /**
     * GitHub 代理地址（单个代理，向后兼容）
     * 支持两种格式：
     * 1. 域名替换模式：gh.llkk.cc (会将 github.com 替换为代理域名)
     * 2. 前缀模式：gh-proxy.com (会在原始 URL 前添加代理前缀)
     */
    private String githubProxy = "gh-proxy.com";
    
    /**
     * GitHub 代理地址列表（多个代理，按优先级降级尝试）
     * 格式：用逗号分隔，例如：gh-proxy.com,hk.gh-proxy.com,cdn.gh-proxy.com
     * 如果配置了此选项，将优先使用此列表；否则使用单个 githubProxy
     */
    private String githubProxyList;
    
    /**
     * 是否启用代理检测和自动转换
     */
    private boolean proxyEnabled = true;
    
    /**
     * 代理检测超时时间（秒）
     */
    private int proxyCheckTimeout = 5;
    
    /**
     * 是否跳过 SSL 证书验证（仅用于开发/测试环境，生产环境不应启用）
     * 默认: false，启用 SSL 验证
     */
    private boolean skipSslVerification = false;

    public String getBaseDir() {
        return baseDir;
    }

    public void setBaseDir(String baseDir) {
        this.baseDir = baseDir;
    }

    public boolean isExtract() {
        return extract;
    }

    public void setExtract(boolean extract) {
        this.extract = extract;
    }
    
    public String getGithubProxy() {
        return githubProxy;
    }
    
    public void setGithubProxy(String githubProxy) {
        this.githubProxy = githubProxy;
    }
    
    public String getGithubProxyList() {
        return githubProxyList;
    }
    
    public void setGithubProxyList(String githubProxyList) {
        this.githubProxyList = githubProxyList;
    }
    
    /**
     * 获取代理列表（优先使用代理列表，否则使用单个代理）
     */
    public java.util.List<String> getProxyHosts() {
        if (githubProxyList != null && !githubProxyList.trim().isEmpty()) {
            return java.util.Arrays.stream(githubProxyList.split(","))
                    .map(String::trim)
                    .filter(s -> !s.isEmpty())
                    .collect(java.util.stream.Collectors.toList());
        }
        // 如果没有配置列表，使用单个代理
        return java.util.Collections.singletonList(githubProxy);
    }
    
    public boolean isProxyEnabled() {
        return proxyEnabled;
    }
    
    public void setProxyEnabled(boolean proxyEnabled) {
        this.proxyEnabled = proxyEnabled;
    }
    
    public int getProxyCheckTimeout() {
        return proxyCheckTimeout;
    }
    
    public void setProxyCheckTimeout(int proxyCheckTimeout) {
        this.proxyCheckTimeout = proxyCheckTimeout;
    }
    
    public boolean isSkipSslVerification() {
        return skipSslVerification;
    }
    
    public void setSkipSslVerification(boolean skipSslVerification) {
        this.skipSslVerification = skipSslVerification;
    }
}


